package com.eastcom.providor.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.remoting.rmi.RmiProxyFactoryBean;

import com.eastcom.bm.atom.Atom_KpiDefine;
import com.eastcom.bm.atom.Atom_NeSpecialThreadhold;
import com.eastcom.bm.atom.Atom_PublicNe;

import com.eastcom.bm.dataprocess.processor.CacheEditBean;
import com.eastcom.notice.NoticeSender;
import com.eastcom.providor.BMSetting;

import com.eastcom.client.TreeNode;
import com.eastcom.dataquality.cc.CacheService;

import com.eastcom.providor.dao.IKpiDefineDao;
import com.eastcom.providor.dao.IPublicNeDao;
import com.eastcom.providor.service.BaseService;
import com.eastcom.providor.service.IKpiDefineService;
import com.eastcom.providor.service.extview.ConfigManagerUtil;

public class KpiDefineService extends BaseService implements IKpiDefineService {

	private IKpiDefineDao kpiDefineDao;
	
	private IPublicNeDao publicNeDao;
	
	private String serviceUrl;
	
	private CacheService service;
	
	public void init(){
		System.setProperty("sun.rmi.dgc.ackTimeout", "10000");
		System.setProperty("sun.rmi.transport.tcp.readTimeout", "10000");
		System.setProperty("sun.rmi.transport.connectionTimeout", "10000");
		RmiProxyFactoryBean rmiFactoryBean = new RmiProxyFactoryBean();
		rmiFactoryBean.setServiceUrl(serviceUrl);
		rmiFactoryBean.setServiceInterface(CacheService.class);
		rmiFactoryBean.setLookupStubOnStartup(false);
		rmiFactoryBean.setRefreshStubOnConnectFailure(true);
		rmiFactoryBean.afterPropertiesSet();
		service = (CacheService) rmiFactoryBean.getObject();
	}
	
	public void setPublicNeDao(IPublicNeDao publicNeDao) {
		this.publicNeDao = publicNeDao;
	}

	public void setKpiDefineDao(IKpiDefineDao kpiDefineDao) {
		this.kpiDefineDao = kpiDefineDao;
	}
	
	public List<TreeNode> getKpiTreeNode(String parentId) {
		List listTreeNode = new ArrayList();
		try {
			if (parentId.equals("root")) {
				for (int i = 0; i < 5; i++) {
					String cityName = "业务"+5;
					TreeNode node = new TreeNode();
					node.setId(cityName);
					node.setText(cityName);
					node.setLeaf(true);
					listTreeNode.add(node);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listTreeNode;
	}


	public List<Atom_KpiDefine> getAllKpiDefine() {
		try{
			//IKpiDefineDao kpiDao=DAOFactory.getInstance().getKpiDefineDao();
			return kpiDefineDao.getAllKpiDefine();
		}
		catch(Exception e){
			log.error("IKpiDefineService getAllKpiDefine error", e);
		}
		/*
		finally{
			JDBCTemplate.doSessionFinally();
		}*/
		
		return null;
	}
	
	public List<Map<String, Object>> getKpiAndSpecialTHByDatasetID(String datasetID){
		try{
			List<Atom_KpiDefine> kpiDefs=null;//=kpiDefineDao.getKpiDefinesByDatasetID(datasetID);
			List<Atom_NeSpecialThreadhold>  neSpecTHs=null;//neSpecialThreadHoldDao.getNeSpecialThreadholdByDatasetID(datasetID);
			List<Atom_PublicNe> pubNes=publicNeDao.getPublicNeByDataSetId(datasetID);
			
			return ConfigManagerUtil.createConfigManagerKpiAndNeSPeTHTable(kpiDefs,neSpecTHs,pubNes);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("kpiDefineDao.getKpiDefinesByDatasetID  || neSpecialThreadHoldDao.getNeSpecialThreadholdByDatasetID is error", e);
		}
		return null;
	}


	public boolean delKpiDefineByDatasetID(String datasetID) {
		try {
			this.kpiDefineDao.delKpiDefineByDatasetID(datasetID);
			return true;
		} 
		catch (Exception e) {
			log.error("KpiDefineService delKpiDefineByDatasetID error", e);
			e.printStackTrace();
		}
		
		return false;
	}

	public boolean delKpiDefineByIds(List<String> Ids)  {
		if(Ids==null||Ids.isEmpty())
			return true;
		
		try {
			this.kpiDefineDao.delKpiDefineByIds(Ids);
			return true;
		} 
		catch (Exception e) {
			log.error("KpiDefineService delKpiDefineByIds error", e);
			e.printStackTrace();
		}
		
		return false;
	}
	
	
	//增加单个指标  TODO
	public boolean saveKpiDefine(Atom_KpiDefine ak) {
		ak.setId(null);
		ak.setCalendarPolicyUUID("002");
		List<Atom_KpiDefine> kpiDefines = new ArrayList<Atom_KpiDefine>();
		kpiDefines.add(ak);
		return saveKpiDefines(kpiDefines);
	}
	
	public boolean saveKpiDefines(List<Atom_KpiDefine> kpiDefines) {
		if(kpiDefines==null||kpiDefines.isEmpty())
			return true;
		
		try {
			for(Atom_KpiDefine kpiDefine:kpiDefines)
				kpiDefine.setCalendarPolicyUUID("002");
			
			this.kpiDefineDao.saveKpiDefines(kpiDefines);
			return true;
		} 
		catch (Exception e) {
			log.error("KpiDefineService saveKpiDefines error", e);
			e.printStackTrace();
		}
		
		return false;
	}

	public List<Atom_KpiDefine> selectKpiDefinesByDatasetID(String datasetID) {
		try{
		    return kpiDefineDao.selectKpiDefinesByDatasetID(datasetID);
		}
		catch(Exception e){
			log.error("KpiDefineService selectKpiDefinesByDatasetID error", e);
		}
		
		return null;
	}


	public Atom_KpiDefine findKpiDefineByDataSetIdAndKpiId(String datasetID, String kpiId) {
		try{
		    return kpiDefineDao.findKpiDefineByDataSetIdAndKpiId(datasetID, kpiId);
		}
		catch(Exception e){
			log.error("KpiDefineService findKpiDefineByDataSetIdAndKpiId error", e);
		}
		
		return null;
	}
	
	//修改单个指标  TODO
	public boolean updateKpiDefine(Atom_KpiDefine ak) {
		ak.setCalendarPolicyUUID("002");
		//String dataSetId = ak.getDatasetDefine().getId();
		//List<Atom_KpiDefine> kpiDefines = new ArrayList<Atom_KpiDefine>();
		//kpiDefines.add(ak);
		try {
			kpiDefineDao.updateKpiDefine(ak);
			return true;
		} 
		catch (Exception e) {
			log.error("KpiDefineService updateKpiDefine error", e);
			e.printStackTrace();
		}
		return false;
		//return updateKpiDefines(dataSetId,kpiDefines);
	}

	public boolean updateKpiDefinebyDataSetId(String dataSetId, Atom_KpiDefine kpiDefine) {
		try {
			if(kpiDefine!=null)
			    kpiDefine.setCalendarPolicyUUID("002");
			
			//1.修改数据库
			kpiDefineDao.updateKpiDefinebyDataSetId(dataSetId, kpiDefine);
			return true;
		} 
		catch (Exception e) {
			log.error("KpiDefineService updateKpiDefine with dataset "+dataSetId+" kpiId "+kpiDefine.getKpiId()+" error", e);
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean rmiUpdateKpiDefinebyDataSetId(String dataSetId, Atom_KpiDefine kpiDefine) {
		try {
			if(updateKpiDefinebyDataSetId(dataSetId, kpiDefine)){
				//2.通知采集端更新缓存
				service.refresh();
				return true;
			}
		} 
		catch (Exception e) {
			log.error("KpiDefineService updateKpiDefine with dataset "+dataSetId+" kpiId "+kpiDefine.getKpiId()+" error", e);
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean updateKpiDefines(String dataSetId, List<Atom_KpiDefine> kpiDefines) {
		try {
			if(kpiDefines!=null){
			    for(Atom_KpiDefine kpiDefine:kpiDefines)
				    kpiDefine.setCalendarPolicyUUID("002");
			}
			
			this.kpiDefineDao.updateKpiDefines(dataSetId, kpiDefines);
			/*
			try{
				CacheEditBean cacheEditBean=new CacheEditBean();
				cacheEditBean.setBeanClass(Atom_KpiDefine.class);
				cacheEditBean.setBeanId(dataSetId);
				
				String operationType=kpiDefines==null||kpiDefines.isEmpty()?"remove":"edit";
				cacheEditBean.setOperationType(operationType);
				NoticeSender.getInstance().sendObject(BMSetting.dataCollectorHost, BMSetting.dataCollectorPort, cacheEditBean, false);
				log.info("KpiDefineService updateKpiDefines send dataSetId "+dataSetId+" to dataCollector completed");
			}
			catch(Exception e){
				log.error("KpiDefineService updateKpiDefines send dataSetId "+dataSetId+" to dataCollector error", e);
			}
			*/
			return true;
		} 
		catch (Exception e) {
			log.error("KpiDefineService updateKpiDefines error", e);
			e.printStackTrace();
		}
		
		return false;
	}

	public String getServiceUrl() {
		return serviceUrl;
	}

	public void setServiceUrl(String serviceUrl) {
		this.serviceUrl = serviceUrl;
	}
}
